package com.zeroext.show.action;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.zeroext.action.common.BaseAction;
import com.zeroext.common.util.JsonUtil;
import com.zeroext.sys.util.PieChartUtil;

@SuppressWarnings("serial")
public class JFreeChartAction extends BaseAction {
    private static final String PIE_CHART_PARAS_NAME = "pieChartParams";
    
    public String saveParms()throws Exception{
        HttpServletRequest request = this.getRequest();
        String key = request.getParameter("key");
        String params = request.getParameter("params");
        this.getSession().setAttribute(key, params);
        JsonUtil.writeSuccessJson(this.getResponse());
        return null;
    }
    
    public String getPieChart()throws Exception{
        String param = (String)this.getSession().getAttribute(PIE_CHART_PARAS_NAME);
        JSONObject config = JSONObject.fromObject(param);
        JSONArray ja = config.getJSONArray("data");
        
        String[] names = new String[ja.size()];
        double[] values = new double[ja.size()];
        String[] colors = new String[ja.size()];
        
        for(int i=0;i<ja.size();i++){
            JSONObject jo = ja.getJSONObject(i);
            names[i] = jo.getString("text");
            values[i] = jo.getDouble("value");
            colors[i] = jo.getString("color");
        }
        
        String title = config.getString("title");
        String unit = config.getString("unit");
        
        int width = config.getInt("width");
        int height = config.getInt("height");
        
        OutputStream os = this.getResponse().getOutputStream();
        
        PieChartUtil.getJPEGPie(os, title, names, values, colors, unit, null, width, height);
        return null;
    }
}
